use eframe::egui;

/// 可拖动的分割线组件
pub struct DraggableSeparator {
    width: f32,
    color: egui::Color32,
}

impl Default for DraggableSeparator {
    fn default() -> Self {
        Self {
            width: 8.0,
            color: egui::Color32::GRAY,
        }
    }
}

impl DraggableSeparator {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_width(mut self, width: f32) -> Self {
        self.width = width;
        self
    }

    pub fn with_color(mut self, color: egui::Color32) -> Self {
        self.color = color;
        self
    }

    /// 显示分割线并返回响应
    pub fn show(&self, ui: &mut egui::Ui, available_height: f32) -> egui::Response {
        // 创建可拖动的区域
        // 获取当前光标位置作为分割线的起始位置
        let cursor_pos = ui.cursor().min;
        let separator_rect =
            egui::Rect::from_min_size(cursor_pos, egui::Vec2::new(self.width, available_height));

        let response = ui.allocate_rect(separator_rect, egui::Sense::drag());

        // 绘制分割线
        if ui.is_rect_visible(separator_rect) {
            let painter = ui.painter();

            // 绘制背景矩形（可拖动区域）
            painter.rect_filled(
                separator_rect,
                egui::CornerRadius::ZERO,
                egui::Color32::from_rgba_unmultiplied(0, 0, 0, 0), // 透明背景
            );

            // 绘制中间的线
            let line_rect = egui::Rect::from_min_size(
                separator_rect.min + egui::Vec2::new(separator_rect.width() / 2.0 - 2.5, 0.0),
                egui::Vec2::new(3.0, separator_rect.height()),
            );
            painter.rect_filled(line_rect, egui::CornerRadius::ZERO, self.color);
        }

        // 设置悬停光标
        if response.hovered() {
            ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeHorizontal);
        }

        response
    }
}
